#include "msgManager.h"



msgManager::msgManager() {
    mgsMAX = 22;
    debug = 0;
}


void msgManager::Init(void) {
    int i;
    for(i=0;i<=mgsMAX;i++) Message[i].Text = "";

    cntMessage = 0;
    MessagePos = 0;


    // load grafics TODO: share one with windowManager or just take some smaller ones
    BITMAP *tmp;
    tmp = load_bitmap("GFX/mapWindow.bmp", NULL);
    if(!tmp) { cout << "load_bitmap [GFX/mapWindow.bmp]" << endl; exit(2); }
    sfWindow = create_video_bitmap(tmp->w, tmp->h);
    blit(tmp, sfWindow, 0, 0, 0, 0, tmp->w, tmp->h);
    destroy_bitmap(tmp);



    // Positionen
    int e;
    for(e=0;e<=17;e++) {
        winPosition[e].x = 1+(e*16);
        winPosition[e].y = 1+(0*16); // 0: Style
        winPosition[e].w = 15;
        winPosition[e].h = 15;
    }
    winPosition[9].w = 47;

    winPosition[10].x = 193;
    winPosition[10].w = 15;


    if(debug>0) Add("msg: System stared");
}


void msgManager::Draw(void) {

    int i;
    int w = 340;
    int x = (int)(SCREEN_W/2)-150;
    int y = (MessagePos*10)+8;

    if(MessagePos!=0) {

        // Fenster
        masked_blit(sfWindow, screen, winPosition[6].x, winPosition[6].y, x,      y-15, winPosition[6].w, winPosition[6].h); // left corner
        masked_blit(sfWindow, screen, winPosition[8].x, winPosition[8].y, x+w-15, y-15, winPosition[8].w, winPosition[8].h); // right corner
        masked_stretch_blit(sfWindow, screen, winPosition[3].x, winPosition[3].y, winPosition[3].w, winPosition[3].h, x,      0,    winPosition[1].w,       y-winPosition[3].h); // left
        masked_stretch_blit(sfWindow, screen, winPosition[5].x, winPosition[5].y, winPosition[5].w, winPosition[5].h, x+w-15, 0,    winPosition[5].w,       y-winPosition[5].h); // right
        masked_stretch_blit(sfWindow, screen, winPosition[7].x, winPosition[7].y, winPosition[7].w, winPosition[7].h, x+15,   y-15, w-(winPosition[7].w*2), winPosition[7].h  ); // down
        masked_stretch_blit(sfWindow, screen, winPosition[4].x, winPosition[4].y, winPosition[4].w, winPosition[4].h, x+15,   0,    w-(winPosition[4].w*2), y-winPosition[4].h); // content
        for(i=0;i<20;i++) textprintf_ex(screen, font, (x+10), (i*10), makecol(255, 255, 255), -1, "%s", Message[i].Text.c_str() );
    }
}


void msgManager::Add(string pText) {

    int i;
    if(MessagePos <= mgsMAX) { // still space
        Message[MessagePos].Text = pText;
        MessagePos++;
        cntMessage = 0;
    } else {                  // move
        cntMessage=0;
        for(i=0;i<mgsMAX;i++)  Message[i] = Message[i+1];
        Message[mgsMAX].Text = pText;
    }
}






void msgManager::Tick(void) {
    cntMessage++;
    int i;
    if(cntMessage>=5) { // X seconds
        cntMessage = 0;
        for(i=0;i<mgsMAX-1;i++)  Message[i] = Message[i+1];
        Message[mgsMAX].Text = "";
        if(MessagePos > 0)MessagePos--;
    }
}


void msgManager::Destroy(void) {



}
